﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Geometry;

namespace SymbolEditor.WpfLibrary
{
    public class BasicGeometryFactory
    {

        internal ESRI.ArcGIS.Client.Geometry.Geometry ProductGeometry(ESRI.ArcGIS.Client.Tasks.GeometryType geometryType, ESRI.ArcGIS.Client.Geometry.Envelope elementEntent)
        {
            Geometry geom = null;
            switch (geometryType)
            {
                case GeometryType.Point: geom = createPoint(elementEntent); break;
                case GeometryType.Polyline: geom = createPolyline(elementEntent); break;
                case GeometryType.Polygon: geom = createPolygon(elementEntent); break;
                default: break;
            }
            return geom;
        }

        private Geometry createPolygon(Envelope elementEntent)
        {
            Polygon polygon = new Polygon();
            polygon.SpatialReference = elementEntent.SpatialReference;
            PointCollection pointCollection = new PointCollection();
            pointCollection.Add(new MapPoint(elementEntent.XMin, elementEntent.YMin));
            pointCollection.Add(new MapPoint(elementEntent.XMin, elementEntent.YMax));
            pointCollection.Add(new MapPoint(elementEntent.XMax, elementEntent.YMax));
            pointCollection.Add(new MapPoint(elementEntent.XMax, elementEntent.YMin));
            pointCollection.Add(new MapPoint(elementEntent.XMin, elementEntent.YMin));
            polygon.Rings.Add(pointCollection);
            return polygon;
        }

        private Geometry createPolyline(Envelope elementEntent)
        {
            PointCollection pointCollection = new PointCollection();
            pointCollection.Add(new MapPoint(elementEntent.XMin, elementEntent.YMax));
            pointCollection.Add(new MapPoint(elementEntent.XMin + elementEntent.Width * 0.75, elementEntent.YMax - elementEntent.Height * 0.25));
            pointCollection.Add(new MapPoint(elementEntent.XMax, elementEntent.YMin));
            Polyline polyline = new Polyline();
            polyline.Paths.Add(pointCollection);
            polyline.SpatialReference = elementEntent.SpatialReference;
            return polyline;
        }

        private Geometry createPoint(Envelope elementEntent)
        {
            return elementEntent.GetCenter();            
        }
    }


}
